package com.primb.bench.project.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.primb.baselibs.util.NetworkUtils;
import com.primb.baselibs.view.AutoToolbar;
import com.primb.bench.R;
import com.primb.bench.base.SwipeBaseActivity;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.module.repository.LoanStockRepository;
import com.primb.bench.common.override.HttpSubscriber;
import com.primb.bench.project.home.entity.LoanCalculateInfoEntity;
import com.primb.netlibs.exception.ApiException;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Chen on 2017/5/23.
 * 功能描述：
 */

public class LoanStockActivity extends SwipeBaseActivity {

    @BindView(R.id.title_bar)
    TextView titleBar;
    @BindView(R.id.tool_bar)
    AutoToolbar toolBar;
    @BindView(R.id.et_stock_number)
    EditText etStockNumber;
    @BindView(R.id.iv_clear_edit)
    ImageView ivClearEdit;
    private CompositeSubscription subscription;
    private int type;

    @Override
    protected void onBaseIntent() {
        super.onBaseIntent();
        Intent intent = getIntent();
        type = intent.getIntExtra("type", AppConstant.LOAN_CALCULATE);
    }

    @Override
    protected int onBaseLayoutId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_loan_stock;
    }

    @Override
    protected void onBaseBindView() {
        toolBar.setNavigationOnClickListener(v -> LoanStockActivity.this.finish());
        if (type == AppConstant.LOAN_CALCULATE) {
            titleBar.setText("贷款续作试算");
        } else {
            titleBar.setText("贷款续作定价");
        }
        etStockNumber.post(() -> {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(etStockNumber, 0);
        });
        etStockNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    ivClearEdit.setVisibility(View.VISIBLE);
                } else {
                    ivClearEdit.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void onLoadData() {
        subscription = new CompositeSubscription();
    }

    private void stockLoan(String text) {
        if (NetworkUtils.isNetworkConnected(this)) {
            showLoadingDialog("加载中");
            Subscription subscribe = LoanStockRepository.getLoanCalculateInfoEntity(text, LoanCalculateActivity.KEY_SCENE_STOCK, LoanCalculateActivity.KEY_PRICE_MEASURE)
                    .subscribe(new HttpSubscriber<LoanCalculateInfoEntity>(this) {
                        @Override
                        public void onCompleted() {
                            hideLoadingDialog();
                        }

                        @Override
                        public void onError(ApiException e) {
                            hideLoadingDialog();
                            showToast(e.getMessage());
                        }

                        @Override
                        public void onNext(LoanCalculateInfoEntity entity) {
                            intentToCalculate(entity);
                        }
                    });
            subscription.add(subscribe);
        }
    }

    private void intentToCalculate(LoanCalculateInfoEntity entity) {
        Intent intent = new Intent(this, LoanCalculateActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("info", entity);
        startActivity(intent);
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        subscription.clear();
    }


    @OnClick({R.id.btn_stock_loan, R.id.iv_clear_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_stock_loan:
                if (!TextUtils.isEmpty(etStockNumber.getText().toString())) {
                    stockLoan(etStockNumber.getText().toString());
                } else {
                    showToast("合同号不能为空");
                }
                break;
            case R.id.iv_clear_edit:
                etStockNumber.setText("");
                ivClearEdit.setVisibility(View.GONE);
                break;
        }
    }
}
